//    Copyright (C) Kherty.  All rights reserved.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Windows.Data;
using System.Windows.Media.Effects;

namespace OpenLS.Drawing.UI
{
    class EffectConverter: IMultiValueConverter
    {
        #region IMultiValueConverter Members

        public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            var colorScheme = StrictConverter.Convert<ColorScheme> (values[0]);
            var effect = StrictConverter.Convert<EffectProperties> (values[1]);
            return ConvertImp(colorScheme, effect);
        }

        private static BitmapEffect ConvertImp(ColorScheme colorScheme, EffectProperties effect)
        {
            if (effect == null)
                return null;
            if (colorScheme == null)
                return null;
            if (effect is EffectList)
                return ConvertList(colorScheme, (EffectList)effect);
            throw new NotImplementedException();;
        }

        private static BitmapEffect ConvertList(ColorScheme colorScheme, EffectList effectList)
        {
            var result = new List<BitmapEffect>();
            if (effectList.Glow != null)
            {
                var e = new OuterGlowBitmapEffect
                            {
                                GlowColor = effectList.Glow.Color.ResolveColor(colorScheme),
                            };
                if (effectList.Glow.Radius != null)
                    e.GlowSize = effectList.Glow.Radius.Value;
                result.Add(e);
            }
            if (effectList.Blur != null)
                throw new NotImplementedException();
            if (effectList.FillOverlay != null)
                throw new NotImplementedException();
            if (effectList.InnerShadow != null)
                throw new NotImplementedException();
            if (effectList.OuterShadow != null)
            {
                var shadow = new DropShadowBitmapEffect
                                                    {
                                                        Color = effectList.OuterShadow.Color.ResolveColor(colorScheme),
                                                        Direction = effectList.OuterShadow.Direction,
                                                        ShadowDepth = effectList.OuterShadow.Distance
                                                    };
                result.Add(shadow);
            }
            if (effectList.PresetShadow != null)
                throw new NotImplementedException();
            if (effectList.Reflection != null)
                throw new NotImplementedException();
            if (effectList.SoftEdge != null)
            {
                Debug.WriteLine("SoftEdge not implemented");

            }
            if (result.Count == 0)
                return null;
            if (result.Count == 1)
            {
                result[0].Freeze();
                return result[0];
            }
            var group = new BitmapEffectGroup();
            foreach (BitmapEffect e in result)
                group.Children.Add(e);
            group.Freeze();
            return group;
        }

        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotSupportedException();
        }

        #endregion
    }
}
